<template>
    <!-- 添加或修改网络对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
    <el-form ref="form" :model="form" :show-message="false" label-width="140px">
      <el-form-item label="网络名称" prop="networkName" required>
        <el-input
          v-model="form.networkName"
          placeholder="请输入网络名称"
          :maxlength="30"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="网络LOGO" prop="networkIcon" required>
        <ImageUpload v-model="form.networkIcon" :width="100" :height="100" />
      </el-form-item>
      <el-form-item label="网络简介" prop="networkProfile">
        <el-input
          v-model="form.networkProfile"
          placeholder="请输入网络简介"
          type="textarea"
          :rows="3"
          :maxlength="300"
        />
      </el-form-item>
      <el-form-item label="网络负责单位" prop="manageCompanyid" required>
        <SelectCompany
          :defaultValue.sync="form.manageCompanyid"
          :companyType="0"
          placeholder="请选择网络负责单位"
          @change="handleChangeCom"
          style="width:100%"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ImageUpload from "@/components/ImageUpload";
import { addNetwork, updateNetwork } from '@/api/mesh/index.js';
import SelectCompany from '@/components/SelectCompany/index';
import { getCompany } from "@/api/company/company";

export default {
  components: { ImageUpload, SelectCompany },
  data() {
    return {
      title: "新建网络",
      open: false,
      form: {
        networkId: undefined,
        networkName: "",
        networkIcon: "",
        networkProfile: "",
        manageCompanyid: "",
        manageCompany: ""
      },
      loading: false,
    };
  },
  methods: {
    openDialog(form) {
      this.title = "新建网络";
      this.form = {};
      Object.assign(this.$data.form, this.$options.data().form);
      this.open = true;

      if (form && form.networkId) {
        this.title = "编辑网络";
        this.form = {
          networkId: form.networkId,
          networkName: form.networkName,
          networkIcon: form.networkIcon,
          networkProfile: form.networkProfile,
          manageCompanyid: form.manageCompanyid,
          manageCompany: form.manageCompany
        };
      }

      this.$nextTick(()=>{
        this.$refs["form"].clearValidate();
      })
    },
    // 选择网络负责单位
    handleChangeCom(comId) {
      if(comId){
        getCompany(comId).then(res=>{
          this.form.manageCompany = res.data.cname;
        })
      }
    },
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            if (this.form.networkId) {
              await updateNetwork(this.form);
              this.$message.success("编辑网络成功");
            } else {
              await addNetwork(this.form);
              this.$message.success("新增网络成功");
            }
            this.$emit("refresh");
            this.loading = false;
            this.open = false;
          } catch (error) {
            this.loading = false
          }
        }
      })
    }
  }
};
</script>

<style lang="scss" scoped></style>
